<?php
namespace Duobao\Controller;

use Duobao\Model\MemberModel as Member;
use Duobao\Model\ShoplistModel as Shoplist;
use Duobao\Model\MemberRecordModel as Record;

class GoodsController extends ControllerBase
{

    /**
     * 默认
     */
    public function index()
    {
        echo "<!--hi , i'm from " . MODULE_NAME . "~" . APP_STATUS, "-->";
    }

    /**
     * 获取商品类别
     */
    public function getCategory(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $model = new Shoplist();

            $category = $model->getGoodsCategory();

            if($category){
                $ret['data']['item'] = $category;
            }
        }catch(\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }


    /**
     * 全部商品
     * 输入：偏移量
     */
    public function allGoods()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try {
            $shopObj = new Shoplist();

            $offset = I('post.offset');//获取偏移量
            $categoryid = I('post.categoryid');
            $length = I('post.limit');//长度使用limit
            $ordername = I('post.ordername');
            $order = I('post.order');//按总需人数排序

            if(empty($offset)){
                $offset = 0;
            }
            if(empty($categoryid)){
                $categoryid = 0;
            }
            if(empty($length)){
                $length = 10;
            }
            if(empty($ordername)){
                $ordername = 1;
            }
            if(empty($order)){
                $item = $shopObj->getAllgoods($categoryid,$offset,$length,$ordername);
            }else{
                $item = $shopObj->orderByTotalNumber($categoryid,$offset,$length,$order);
            }

            if ($item) {
                $ret['data']['item'] = $item;
            }
        } catch (\Exception $e) {
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }


    /**
     * 根据商品id获取商品信息
     * 输入：商品id
     */
    public function getGoodsById()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try {
            $shopObj = new Shoplist();

            $id = I('post.id');//商品id

            $item = $shopObj->getGoodsById($id);
            if ($item) {
                $ret['data'] = $item;
            }
        } catch (\Exception $e) {
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 获取商品的图文详情
     * 输入商品：id
     */
    public function getGoodContentById(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $goods = new Shoplist();

            $id = I('post.id');

            $item = $goods->getGoodContentById($id);
            $ret['data']['content'] = $item;
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 当前商品id的往期商品
     */
    public function previousRecord(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $model = new Shoplist();

            $id = I('post.id');

            if(!empty($id)){
                $result =  $model->previousRecord($id);

                if($result){
                    $ret['data']['item'] = $result;
                }else{
                    $ret['msg'] = 'The current commodity no past';#当前商品没有往期
                }
            }else{
                $ret['status'] = '403';
                $ret['msg'] = 'Gets the parameters ID is empty';//获取的参数id为空
            }

        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 商品的参与记录,商品id
     */
    public function inRecordOfGoods(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            $id = I('post.id');
            $offset =I('post.offset');//偏移量
            $length = I('post.limit');//长度

            //判断offset与length 如果都不存在给定默认值
            if(empty($offset)){
                $offset = 0;
            }
            if(empty($length)){
                $length = 0;
            }

            if(!empty($id) && is_numeric($id)){
                $model = new Record();
                $result = $model->inRecordOfGoods($id,$offset,$length);
                //判断数据库查询结果是否为空
                if($result){
                    $ret['data']['item'] = $result;
                }else{
                    $ret['data'] = '';
                    $ret['msg'] = 'database does not exsit';#数据库不存在当前前进的数据
                }
            }else{
                $ret['status'] = '403';
                $ret['msg'] = 'Gets the parameters ID is empty or ID is not a number';//获取的参数id为空
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }

    /**
     * 查看计算详情
     * 已揭晓的商品查看计算详情
     * 获取已揭晓的商品的id
     */
    public function checkCalculateDetail(){
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();

        try{
            #获取已揭晓的商品id
            $gid = I('get.gid');
            if(!empty($gid)){
                $model = new Shoplist();
                $result = $model->checkCalculateDetail(intval($gid));
                if($result){
                    $ret['data'] = $result;
                }else{
                    $ret['status'] = 500;
                    $ret['msg'] = '商品不存在或者还未揭晓';
                }
            }else{
                $ret['status'] = 403;
                $ret['msg'] = 'goods id is empty';
            }
        }catch (\Exception $e){
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }

        return $this->response($ret);
    }
    /** end of Goods controller**/
}
